package com.yuhong.springbootinit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuhong.springbootinit.model.dto.invitation.InvitationQueryRequest;
import com.yuhong.springbootinit.common.ErrorCode;
import com.yuhong.springbootinit.constant.CommonConstant;
import com.yuhong.springbootinit.exception.BusinessException;
import com.yuhong.springbootinit.exception.ThrowUtils;
import com.yuhong.springbootinit.model.entity.Invitation;
import com.yuhong.springbootinit.model.vo.InvitationVO;
import com.yuhong.springbootinit.service.InvitationService;
import com.yuhong.springbootinit.mapper.InvitationMapper;
import com.yuhong.springbootinit.utils.SqlUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* @author 10467
* @description 针对表【invitation】的数据库操作Service实现
* @createDate 2025-02-22 13:52:10
*/
@Service
public class InvitationServiceImpl extends ServiceImpl<InvitationMapper, Invitation>
    implements InvitationService{

    @Override
    public void validInvitation(Invitation invitation, boolean add) {
        if (invitation == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        String hrId = invitation.getHrId();
        String hrName = invitation.getHrName();
        String userId = invitation.getUserId();
        String companyId = invitation.getCompanyId();
        String positionId = invitation.getPositionId();
        String companyName = invitation.getCompanyName();

        // 创建时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(hrId, hrName, userId,companyId,positionId,companyName), ErrorCode.PARAMS_ERROR);
        }
        // 有参数则校验
        if (StringUtils.isNotBlank(hrId) && hrId.length() > 80) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "id过长");
        }
        if (StringUtils.isNotBlank(hrName) && hrName.length() > 8192) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "内容过长");
        }

    }

    @Override
    public InvitationVO getInvitationVO(Invitation invitation, HttpServletRequest request) {

        InvitationVO invitationVO = InvitationVO.objToVo(invitation);

        return invitationVO;
    }

    @Override
    public Page<InvitationVO> getInvitationVOPage(Page<Invitation> invitationPage, HttpServletRequest request) {
        List<Invitation> invitationList = invitationPage.getRecords();
        Page<InvitationVO> invitationVOPage = new Page<>(invitationPage.getCurrent(), invitationPage.getSize(), invitationPage.getTotal());
        if (CollUtil.isEmpty(invitationList)) {
            return invitationVOPage;
        }
        List<InvitationVO> invitationVOS = BeanUtil.copyToList(invitationList, InvitationVO.class);
        invitationVOPage.setRecords(invitationVOS);
//        // 1. 关联查询用户信息
//        Set<Long> userIdSet = postList.stream().map(Position::getUserId).collect(Collectors.toSet());
//        Map<Long, List<User>> userIdUserListMap = userService.listByIds(userIdSet).stream()
//                .collect(Collectors.groupingBy(User::getId));
//        // 2. 已登录，获取用户点赞、收藏状态
//        Map<Long, Boolean> postIdHasThumbMap = new HashMap<>();
//        Map<Long, Boolean> postIdHasFavourMap = new HashMap<>();
//        User loginUser = userService.getLoginUserPermitNull(request);
//        if (loginUser != null) {
//            Set<Long> postIdSet = postList.stream().map(Post::getId).collect(Collectors.toSet());
//            loginUser = userService.getLoginUser(request);
//            // 获取点赞
//            QueryWrapper<PostThumb> postThumbQueryWrapper = new QueryWrapper<>();
//            postThumbQueryWrapper.in("postId", postIdSet);
//            postThumbQueryWrapper.eq("userId", loginUser.getId());
//            List<PostThumb> postPostThumbList = postThumbMapper.selectList(postThumbQueryWrapper);
//            postPostThumbList.forEach(postPostThumb -> postIdHasThumbMap.put(postPostThumb.getPostId(), true));
//            // 获取收藏
//            QueryWrapper<PostFavour> postFavourQueryWrapper = new QueryWrapper<>();
//            postFavourQueryWrapper.in("postId", postIdSet);
//            postFavourQueryWrapper.eq("userId", loginUser.getId());
//            List<PostFavour> postFavourList = postFavourMapper.selectList(postFavourQueryWrapper);
//            postFavourList.forEach(postFavour -> postIdHasFavourMap.put(postFavour.getPostId(), true));
//        }
        // 填充信息
//        List<PostVO> postVOList = postList.stream().map(post -> {
//            PostVO postVO = PostVO.objToVo(post);
//            Long userId = post.getUserId();
//            User user = null;
//            if (userIdUserListMap.containsKey(userId)) {
//                user = userIdUserListMap.get(userId).get(0);
//            }
//            postVO.setUser(userService.getUserVO(user));
//            postVO.setHasThumb(postIdHasThumbMap.getOrDefault(post.getId(), false));
//            postVO.setHasFavour(postIdHasFavourMap.getOrDefault(post.getId(), false));
//            return postVO;
//        }).collect(Collectors.toList());
//        postVOPage.setRecords(postVOList);
        return invitationVOPage;
    }

    @Override
    public QueryWrapper<Invitation> getQueryWrapper(InvitationQueryRequest invitationQueryRequest) {
        QueryWrapper<Invitation> queryWrapper = new QueryWrapper<>();
        if (invitationQueryRequest == null) {
            return queryWrapper;
        }

        String hrId = invitationQueryRequest.getHrId();
        String hrName = invitationQueryRequest.getHrName();
        String userId = invitationQueryRequest.getUserId();
        String companyId = invitationQueryRequest.getCompanyId();
        String positionId = invitationQueryRequest.getPositionId();
        String companyName = invitationQueryRequest.getCompanyName();
        int current = invitationQueryRequest.getCurrent();
        int pageSize = invitationQueryRequest.getPageSize();
        String sortField = invitationQueryRequest.getSortField();
        String sortOrder = invitationQueryRequest.getSortOrder();

        // 拼接查询条件
        if (StringUtils.isNotBlank(companyName)) {
//            queryWrapper.and(qw -> qw.like("title", location).or().like("content", location));
            queryWrapper.like("company_name",companyName);
        }
        queryWrapper.like(StringUtils.isNotBlank(hrName), "hr_name", hrName);
        queryWrapper.eq(ObjectUtils.isNotEmpty(positionId), "position_id", positionId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(hrId), "hr_id", hrId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(companyId), "company_id", companyId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "user_id", userId);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }
}




